#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QFileDialog>
#include<QFile>
#include<QMessageBox>
#include<QTextStream>
#include<QFontDialog>



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("无标题--记事本"));
    connect(ui->action_OpenFile,SIGNAL(triggered()),this,SLOT(openfile_on()));
    connect(ui->action_Close,SIGNAL(triggered()),this,SLOT(quitfile()));
    connect(ui->action_About,SIGNAL(triggered()),this,SLOT(aboutfile()));
    connect(ui->action_SaveAs,SIGNAL(triggered()),this,SLOT(saveAs()));
    connect(ui->action_NewFile,SIGNAL(triggered()),this,SLOT(new_on()));
    connect(ui->action_TypeFace,SIGNAL(triggered()),this,SLOT(font_on()));
}


//********************************************************************************
//槽函数实现：openFile_on
//********************************************************************************

void MainWindow::openfile_on()
{
      QTextDocument *document=ui->plainTextEdit->document();
      if(document->isModified()){
          int reg=QMessageBox::question(this,tr("保存？"),
                                        tr("请选择是否保存？"),
                                        QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
          if(reg==QMessageBox::Cancel)
              return;
          else if(reg==QMessageBox::Save){
              savefile(this->curFile);
          }

      }
      QString filename=QFileDialog::getOpenFileName(this,tr("打开文件"),".",tr("Txt Files(*.txt)"));
      if(filename.isEmpty())
          return;
        this->openfile(filename);
      this->changetitle(filename);
}

//********************************************************************************
//槽函数实现：savefile
//********************************************************************************
bool MainWindow::save()
{
    if(this->curFile.isEmpty())
       return saveAs();
    else
        savefile(this->curFile);
    return true;
}
//********************************************************************************
//槽函数实现：saveAs
//********************************************************************************

bool MainWindow::saveAs(){
    QString fileName=QFileDialog::getSaveFileName(this,tr("文件存储"),".",tr("Txt File(*.txt)"));
    if(fileName==0)
        return false;
    this->savefile(fileName);
    return 1;
}

//********************************************************************************
//函数实现：savefile
//********************************************************************************
void MainWindow::savefile(const QString &filename)
{
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
            QMessageBox::warning(this, "Save",
                                 tr("Save , error ?")
                                 .arg(filename)
                                .arg(file.errorString()));
            return ;
        }
    QTextStream out(&file);
    out<<ui->plainTextEdit->toPlainText();
    file.close();

}

//********************************************************************************
//函数实现：changetitle
//********************************************************************************

void MainWindow::changetitle(const QString &filename){
    if(filename.isEmpty()){
        this->curFile="";
        this->setWindowTitle(tr("无标题。txt【*】--记事本"));
    }else {
        this->curFile=filename;
        QFileInfo fi(filename);
        QString name=fi.fileName();
        this->setWindowTitle(tr("%1[*]--记事本").arg(name));
    }
  //  this->setWindowModality(false);
    ui->plainTextEdit->document()->setModified(false);
}

//********************************************************************************
//函数实现：openfile()
//
void MainWindow::openfile(const QString &filename){
    QFile file(filename);
    //打开文件流
    file.open(QIODevice::ReadOnly|QIODevice::Text);

    QTextStream in(&file);

    QString s=in.readAll();

    ui->plainTextEdit->setPlainText(s);
}



//********************************************************************************
//槽函数实现：quitfile
//********************************************************************************
void MainWindow::quitfile()
{
    if(!QMessageBox::information(this,tr("关闭对话框"),tr("真的想关闭对话框吗？"),tr("是的"),tr("不是")))
        this->close();
}

//********************************************************************************
//槽函数实现：new_on
//********************************************************************************
void MainWindow::new_on()
{
    QTextDocument *document=ui->plainTextEdit->document();
    if(!document->isModified()){
        ui->plainTextEdit->setPlainText("");
        this->changetitle("");
        return;
    }
    int ret=QMessageBox::information(this,tr("保存?"),
                                                                            "是否保存？",
                                                                             QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel);
    switch(ret)
    {
        case QMessageBox::Cancel:
                 break;
        case QMessageBox::Discard:
                ui->plainTextEdit->setPlainText("");
                this->changetitle("");
                break;
        case QMessageBox::Save:
                if(!save())
                    return;
                ui->plainTextEdit->setPlainText("");
                this->changetitle("");
                break;
    }
}


//********************************************************************************
//事件实现：closeEvent
//关闭的:QMessageBox 有两种，一种是构造函数 另一种是静态函数
//********************************************************************************
void MainWindow::closeEvent(QCloseEvent *event)
{
    QMessageBox::StandardButton rb = QMessageBox::question(NULL, "关闭程序", "你确定要关闭成吗?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
    if(rb == QMessageBox::Yes)
    {
            event->accept();
    }else if(rb==QMessageBox::No)
        event->ignore();

}
//********************************************************************************
//实现槽函数：about
//********************************************************************************

void MainWindow::aboutfile()
{
    QMessageBox::aboutQt(NULL,"about QT");
}

//********************************************************************************
//实现槽函数：font_on
//********************************************************************************
void MainWindow::font_on()
{
    bool ok;
    const QFont  fa=QFontDialog::getFont(&ok,
                                           ui->plainTextEdit->font(),
                                            this,
                                            tr("字体选择"));
    if(ok)
    {
        ui->plainTextEdit->setFont(fa);
    }

}





MainWindow::~MainWindow()
{
    delete ui;
}






